package com.xiao.mall.foundation.resource;

import com.xiao.mall.foundation.service.MetaDataService;
import com.xiao.mall.foundation.service.dto.EnumType;
import com.xiao.mall.foundation.web.util.PaginationUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhoushaofeng
 */
@RestController
@RequestMapping("/meta-data")
@Slf4j
@Api(tags = "元数据")
public class MetaDataResource {

    private final MetaDataService metaDataService;

    public MetaDataResource(MetaDataService metaDataService) {
        this.metaDataService = metaDataService;
    }

    @GetMapping("enums")
    public ResponseEntity<Map<String, Object[]>> findAllEnumObjects() {
        List<EnumType> enumTypes = metaDataService.findAllEnumObjects(new String[] {"com.xiao.mall"});

        if (log.isDebugEnabled()) {
            log.debug("found enumTypes: {}", enumTypes);
        }

        HttpHeaders headers = PaginationUtil.generateListHttpHeaders(enumTypes);
        return ResponseEntity.ok()
            .headers(headers)
            .body(enumTypes
                .stream()
                .collect(Collectors.toMap(EnumType::getName, EnumType::getValues)));
    }
}
